package resource.handler;

import java.util.ArrayList;

import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.util.Log;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;

import character.Hero;
import character.PNJ;

/**
 * Parser SAX
 * @author PQMY07891
 *
 */
public class GameCharacterHandler extends DefaultHandler {

	private ArrayList<Hero> l_gc;
	private String position;
	private ArrayList<Image> one_movement;
	private Image no_mov;
	
	private ArrayList<String> list_action = new ArrayList<String>();
	
	public GameCharacterHandler(ArrayList<Hero> s){
		l_gc = s;
	}

	public void characters(char[] buffer, int start, int length) {
	}

	public void startElement(String uri, String name, String qName, Attributes attributes){
		if (qName.equals("hero")){
			try {
				Log.info("Chargement Personnage : " + attributes.getValue("name"));
				l_gc.add(new Hero(attributes.getValue("name"),attributes.getValue("url"),Integer.parseInt(attributes.getValue("width")),Integer.parseInt(attributes.getValue("height")),0,0));
			} catch (NumberFormatException e) {
				e.printStackTrace();
			} catch (SlickException e) {
				e.printStackTrace();
			}
		};

		if (qName.equals("pnj")){
			try {
				Log.info("Chargement PNJ : " + attributes.getValue("name"));
				l_gc.add(new PNJ(attributes.getValue("name"),attributes.getValue("url"),Integer.parseInt(attributes.getValue("width")),Integer.parseInt(attributes.getValue("height")),0,0,attributes.getValue("default_value")));
			} catch (NumberFormatException e) {
				e.printStackTrace();
			} catch (SlickException e) {
				e.printStackTrace();
			}
		};
		
		if (qName.equals("movement")){
			position = attributes.getValue("type");
			one_movement = new ArrayList<Image>();
		};
		
		if(qName.equals("sprite")){
			try {
				one_movement.add(l_gc.get(l_gc.size()-1).getSprite(Integer.parseInt(attributes.getValue("x")),Integer.parseInt(attributes.getValue("y"))));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		if(qName.equals("standing")){
			try {
				no_mov = l_gc.get(l_gc.size()-1).getSprite(Integer.parseInt(attributes.getValue("x")),Integer.parseInt(attributes.getValue("y")));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		if (qName.equals("interaction")){
			list_action.add(attributes.getValue("name"));
		}
	}

	// When the parser encounters the end of an element, it calls this method
	public void endElement(String uri, String name, String qName){
		if (qName.equals("movement")){
			l_gc.get(l_gc.size()-1).AnimateMovement(position, one_movement, no_mov);
		}
		
		if (qName.equals("pnj")){
			if(list_action.size() > 0){
				PNJ pnj = (PNJ) l_gc.get(l_gc.size()-1);
				pnj.setInteractions(list_action);
				list_action = new ArrayList<String>();
			}
		}
	}

	/** This method is called when warnings occur */
	public void warning(SAXParseException exception) {
		System.err.println("WARNING: line " + exception.getLineNumber() + ": "
				+ exception.getMessage());
	}

	/** This method is called when errors occur */
	public void error(SAXParseException exception) {
		System.err.println("ERROR: line " + exception.getLineNumber() + ": "
				+ exception.getMessage());
	}

	/** This method is called when non-recoverable errors occur. */
	public void fatalError(SAXParseException exception) throws SAXException {
		System.err.println("FATAL: line " + exception.getLineNumber() + ": "
				+ exception.getMessage());
		throw (exception);
	}
}